/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.mc.sdk.filecenter.common.enums;

import cn.hallele.infrastructure.data.converter.EnumConverter;
import cn.hallele.infrastructure.util.EnumUtil;
import lombok.Getter;

import java.util.List;

/**
 * hallele_mc_sdk
 *
 * @author anle5
 * @since 2024/9/22 19:37
 */
@Getter
public enum FileTypeEnum implements EnumConverter<Integer> {
    /**
     * Office类的文件，如：Excel、PPT、Word
     */
    OFFICE(1),

    /**
     * 图片类文件，如：png、img、jpg等
     */
    IMG(2),

    /**
     * PDF文件
     */
    PDF(3),

    /**
     * 密钥类
     */
    KEY(4),

    /**
     * 位置
     */
    UNKNOWN(10);

    private final static List<String> OFFICE_SUFFIX = List.of(new String[]{"xlsx", "xls", "doc", "docx", "ppt"});
    private final static List<String> IMG_SUFFIX = List.of(new String[]{"img", "png", "ico", "gif", "jpg"});
    private final static List<String> PDF_SUFFIX = List.of(new String[]{"pdf"});
    private final static List<String> KEY_SUFFIX = List.of(new String[]{"key", "keystore"});

    /**
     * 状态标识
     */
    private final Integer code;

    FileTypeEnum(Integer code) {
        this.code = code;
    }

    /**
     * 转换成实体字段值
     *
     * @param columValue 表字段里面的值
     * @return name
     */
    @Override
    public EnumConverter<Integer> convertToEntityAttribute(Integer columValue) {
        return EnumUtil.getBy((EnumUtil.HalFunction<FileTypeEnum, Integer>) param -> param.code, columValue);
    }

    /**
     * 转换成表字段值
     *
     * @return 表字段里面的值
     */
    @Override
    public Integer convertToDatabaseColumn() {
        return this.code;
    }

    public static FileTypeEnum getByName(String name) {
        if (OFFICE_SUFFIX.contains(name)) {
            return OFFICE;
        }

        if (IMG_SUFFIX.contains(name)) {
            return IMG;
        }

        if (PDF_SUFFIX.contains(name)) {
            return PDF;
        }
        if (KEY_SUFFIX.contains(name)) {
            return KEY;
        }
        return UNKNOWN;
    }
}
